home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / mac / files / t_sys5 / unixcpio.gz / unixnet.cpio / brk.c < prev    next >
C/C++ Source or Header  |  1994-07-11  |  4KB  |  135 lines

  1. /* This is a reverse-compilation of Borland's brk() and sbrk() functions.
  2. ** Borland would prefer that I distribute these things on an object-only
  3. ** basis, but since this is a bug fix that they knew about on version 1.0
  4. ** and DIDN'T fix on version 1.5, to heck with them.
  5. **
  6. ** This is only needed in small data models, hence the leading #ifdef.
  7. ** The fix lets us define the so-called red zone, which is the buffer
  8. ** space between the bottom of the heap and the top of the stack.  Also
  9. ** fixes a bug wherein brk() returned the new brklvl on success.  Wrong!
  10. **
  11. ** If you '#define original', you'll get Borland's original code.
  12. */
  13.  
  14. #if defined(__TINY__) || defined(__SMALL__) || defined(__MEDIUM__)
  15.  
  16. #pragma inline
  17.  
  18. extern int errno;
  19. extern int __brklvl;
  20.  
  21. #ifdef original
  22. #define _STKRED 0x100
  23. #else
  24. extern int _STKRED;
  25. #endif
  26.  
  27. int __brk(unsigned bar)
  28. {
  29.     _AX = bar;
  30.     asm    mov    dx,sp
  31.     _DX -= _STKRED;
  32.     if (_AX < _DX) {
  33.         __brklvl = _AX;
  34. #ifndef original
  35.         return 0;
  36. #endif
  37.     }
  38.     else {
  39.         errno = 8;
  40.         _AX = -1;
  41.     }
  42. }
  43.  
  44. /*
  45. Original from Turbo C's cm library:
  46. BRK_TEXT:___brk:
  47. 3999:0001 55             PUSH    BP 
  48. 3999:0002 8BEC           MOV    BP,SP 
  49. 3999:0004 8B4606         MOV    AX,[BP+06] 
  50. 3999:0007 8BD4           MOV    DX,SP 
  51. 3999:0009 81EA0001       SUB    DX,0100 
  52. 3999:000D 3BC2           CMP    AX,DX 
  53. 3999:000F 7305           JNB    ___brk+15 (0016) 
  54. 3999:0011 A38E00         MOV    [008E],AX 
  55. 3999:0014 EB09           JMP    ___brk+1E (001F) 
  56. 3999:0016 C70688000800   MOV    Word Ptr [0088],0008 
  57. 3999:001C B8FFFF         MOV    AX,FFFF 
  58. 3999:001F 5D             POP    BP 
  59. 3999:0020 CB             RETF
  60. */
  61.  
  62. __sbrk(unsigned long bar)
  63. {
  64.     asm    mov    ax,[bp+06] 
  65.     asm    mov    dx,[bp+08] 
  66.     asm    add    ax,__brklvl
  67.     asm    adc    dx,0
  68.     asm    mov    cx,ax
  69.     _CX += _STKRED;
  70.     asm    adc    dx,0
  71.     if (_DX == 0 && _CX < _SP) asm xchg ax,__brklvl;
  72.     else {
  73.         errno = 8;
  74.         _AX = -1;
  75.     }
  76. }
  77.  
  78. /*
  79. Original from Turbo C's cm library:
  80. BRK_TEXT:___sbrk:
  81. 3999:0021 55             PUSH    BP 
  82. 3999:0022 8BEC           MOV    BP,SP 
  83. 3999:0024 8B4606         MOV    AX,[BP+06] 
  84. 3999:0027 8B5608         MOV    DX,[BP+08] 
  85. 3999:002A 03068E00       ADD    AX,[008E] 
  86. 3999:002E 83D200         ADC    DX,+00 
  87. 3999:0031 8BC8           MOV    CX,AX 
  88. 3999:0033 81C10001       ADD    CX,0100 
  89. 3999:0037 83D200         ADC    DX,+00 
  90. 3999:003A 0BD2           OR    DX,DX 
  91. 3999:003C 750A           JNZ    ___sbrk+27 (0048) 
  92. 3999:003E 3BCC           CMP    CX,SP 
  93. 3999:0040 7306           JNB    ___sbrk+27 (0048) 
  94. 3999:0042 87068E00       XCHG    AX,[008E] 
  95. 3999:0046 EB09           JMP    ___sbrk+30 (0051) 
  96. 3999:0048 C70688000800   MOV    Word Ptr [0088],0008 
  97. 3999:004E B8FFFF         MOV    AX,FFFF 
  98. 3999:0051 5D             POP    BP 
  99. 3999:0052 CB             RETF     
  100. */
  101.  
  102. int brk(unsigned bar) {return __brk(bar);}
  103.  
  104. /*
  105. Original from Turbo C's cm library:
  106. BRK_TEXT:_brk:
  107. 3999:0053 55             PUSH    BP 
  108. 3999:0054 8BEC           MOV    BP,SP 
  109. 3999:0056 FF7606         PUSH    [BP+06] 
  110. 3999:0059 0E             PUSH    CS 
  111. 3999:005A E8A4FF         CALL    ___brk 
  112. 3999:005D 59             POP    CX 
  113. 3999:005E 5D             POP    BP 
  114. 3999:005F CB             RETF     
  115. */
  116.  
  117. sbrk(int bar) {return __sbrk(bar);}
  118.  
  119. /*
  120. Original from Turbo C's cm library:
  121. BRK_TEXT:_sbrk:
  122. 3999:0060 55             PUSH    BP 
  123. 3999:0061 8BEC           MOV    BP,SP 
  124. 3999:0063 8B4606         MOV    AX,[BP+06] 
  125. 3999:0066 99             CWD     
  126. 3999:0067 52             PUSH    DX 
  127. 3999:0068 50             PUSH    AX 
  128. 3999:0069 0E             PUSH    CS 
  129. 3999:006A E8B4FF         CALL    ___sbrk 
  130. 3999:006D 8BE5           MOV    SP,BP 
  131. 3999:006F 5D             POP    BP 
  132. 3999:0070 CB             RETF     
  133. */
  134. #endif
  135.